package com.tsing.cedap.rest.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.tsing.cedap.common.config.RedisManager;
import com.tsing.cedap.common.constants.AuthConstant;
import com.tsing.cedap.common.utils.GetLoginUserUtil;
import com.tsing.cedap.common.vo.UserVo;
import com.tsing.cedap.rest.entity.CampusEntity;
import com.tsing.cedap.rest.entity.CollegeEntity;
import com.tsing.cedap.rest.entity.SchoolEntity;
import com.tsing.cedap.rest.entity.UserEntity;
import com.tsing.cedap.rest.mapper.*;
import com.tsing.cedap.rest.service.IListService;
import com.tsing.cedap.rest.vo.reqVo.CodeReqVo;
import com.tsing.cedap.rest.vo.respVo.CampusTreeRespVo;
import com.tsing.cedap.rest.vo.respVo.IdAndNameRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author TheTsing
 * @create 2022-01-26 11:38
 */
@Service
public class ListServiceImpl implements IListService {

    @Autowired
    private RedisManager redisManager;

    @Resource
    private SchoolMapper schoolMapper;

    @Resource
    private CampusMapper campusMapper;

    @Resource
    private CollegeMapper collegeMapper;

    @Resource
    private CourierStationMapper stationMapper;

    @Resource
    private ReceivingAddressMapper addressMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<IdAndNameRespVo> schoolList() {
        List<SchoolEntity> schoolEntities = new LambdaQueryChainWrapper<>(schoolMapper)
                .select(SchoolEntity::getId, SchoolEntity::getName)
                .orderByDesc(SchoolEntity::getId)
                .list();
        return BeanUtil.copyToList(schoolEntities, IdAndNameRespVo.class);
    }

    @Override
    public List<IdAndNameRespVo> campusList(CodeReqVo reqVo) {
        List<CampusEntity> campusEntities = new LambdaQueryChainWrapper<>(campusMapper)
                .select(CampusEntity::getId, CampusEntity::getName)
                .eq(CampusEntity::getSchoolId, reqVo.getCode())
                .orderByDesc(CampusEntity::getId)
                .list();
        return BeanUtil.copyToList(campusEntities, IdAndNameRespVo.class);
    }

    @Override
    public List<IdAndNameRespVo> collegeList(CodeReqVo reqVo) {
        List<CollegeEntity> collegeEntities = new LambdaQueryChainWrapper<>(collegeMapper)
                .select(CollegeEntity::getId, CollegeEntity::getName)
                .eq(CollegeEntity::getCampusId, reqVo.getCode())
                .orderByDesc(CollegeEntity::getId)
                .list();
        return BeanUtil.copyToList(collegeEntities, IdAndNameRespVo.class);
    }

    @Override
    public List<CampusTreeRespVo> campusTree() {
        return campusMapper.campusTree();
    }

    @Override
    public List<CampusTreeRespVo> courierStationTree(CodeReqVo reqVo) {
        return stationMapper.courierStationTree(reqVo.getCode());
    }

    @Override
    public List<IdAndNameRespVo> addressList(HttpServletRequest request) {
        String token = request.getHeader(AuthConstant.AUTHENTICATION_HEAD);
        UserVo userVo = GetLoginUserUtil.getLoginUserInfo(token, redisManager);
        return addressMapper.addressList(userVo.getId());
    }

    @Override
    public List<IdAndNameRespVo> riderList(CodeReqVo reqVo) {
        return campusMapper.riderList(reqVo.getCode());
    }

    @Override
    public List<IdAndNameRespVo> adminPhoneList(CodeReqVo reqVo) {
        return campusMapper.adminPhoneList(reqVo.getCode());
    }

    @Override
    public List<IdAndNameRespVo> adminList() {
        List<UserEntity> userEntities = new LambdaQueryChainWrapper<>(userMapper)
                .select(UserEntity::getId, UserEntity::getName)
                .eq(UserEntity::getRole, 1)
                .list();
        return BeanUtil.copyToList(userEntities, IdAndNameRespVo.class);
    }

}
